package com.foodmarket.controller;
import com.foodmarket.pojo.Result;
import com.foodmarket.utils.AliOssutil;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {

    @SneakyThrows
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws  IOException{

        String originalFileName=file.getOriginalFilename();
        String filename= UUID.randomUUID().toString()+originalFileName.substring(originalFileName.lastIndexOf("."));
//        file.transferTo(new File(""+filename));
        String url = AliOssutil.uploadFile(filename,file.getInputStream());
        return Result.success(url);
    }
}
